import 'package:FineArt/constants.dart';
import 'package:FineArt/core/http/base_dio.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:retrofit/retrofit.dart';
import 'package:dio/dio.dart';
import '../../constants.dart';

part 'module_api.g.dart';

@RestApi(baseUrl: kBaseUrl)
abstract class ModuleApi {
  factory ModuleApi({Dio dio, String baseUrl}) {
    dio ??= BaseDio.getInstance().getDio();
    return _ModuleApi(dio, baseUrl: baseUrl);
  }

  @GET("/api/v1/modules") //获取板块列表
  Future<ModulesResponse> modules();

  @GET("/api/v1/module") //获取板块列表
  Future<GlobalSearchResponse> globalSearch(@Query("param") String param);

  @GET("/api/v1/module/{id}/{pid}") //获取分类列表
  Future<CategoriesResponse> categories(
      @Path("id") int id, @Path("pid") int pid);

  @GET("/api/v1/module/{id}/{pid}/filters") //获取指定板块的更多标签
  Future<FiltersResponse> filters(@Path("id") int id, @Path("pid") int pid);

  @GET("/api/v1/asset") //拉取资源
  Future<ResourcesResponse> search(
      @Query("p") String path,
      @Query("f") String filter,
      @Query("pg") int page,
      @Query("size") int size);

  @GET("/api/v1/asset") //拉取资源
  Future<ResourcesResponse> search1(
      @Query("p") String path,
      @Query("f") String filter,
      @Query("r") String remark,
      @Query("pg") int page,
      @Query("size") int size);
// @GET("/api/v1/asset/{id}/{tp}") //获取指定资源的详细信息（包括高清，图书图片序列等相关信息）
// Future<ResourceDetailResponse> resourceDetail();

  @GET("/api/v1/module/{id}/{pid}/more")
  Future<ModuleMoreResponse> moreFilters(
      @Path("id") int id, @Path("pid") int pid, @Query("path") String path);
}

@JsonSerializable()
class ModuleMoreResponse {
  String requestId;
  int code;
  String msg;
  MoreFilterGroup data;

  ModuleMoreResponse({this.requestId, this.code, this.msg, this.data});

  factory ModuleMoreResponse.fromJson(Map<String, dynamic> json) =>
      _$ModuleMoreResponseFromJson(json);
}

@JsonSerializable()
class MoreFilterGroup {
  @JsonKey(defaultValue: "全部")
  String group_label;

  List<MoreFilter> group_items;

  @JsonKey(defaultValue: 0)
  int level;

  @JsonKey(defaultValue: false)
  bool asFilterObject; //显示的时候，把Filter对象当成此对象显示

  @JsonKey(defaultValue: true)
  bool isExpand;

  MoreFilterGroup({this.group_label, this.group_items});

  factory MoreFilterGroup.fromJson(Map<String, dynamic> json) =>
      _$MoreFilterGroupFromJson(json);
}

@JsonSerializable()
class MoreFilter {
  String label;
  int id;
  String path;
  MoreFilterGroup children;
  List<Filter> filters;

  @JsonKey(defaultValue: false)
  bool selected;

  MoreFilter({this.label, this.id, this.path, this.children, this.filters});
  factory MoreFilter.fromJson(Map<String, dynamic> json) =>
      _$MoreFilterFromJson(json);
}

//////
@JsonSerializable()
class GlobalSearchResponse {
  String requestId;
  int code;
  String msg;
  List<GlobalSearch> data;

  GlobalSearchResponse({this.requestId, this.code, this.msg, this.data});

  factory GlobalSearchResponse.fromJson(Map<String, dynamic> json) =>
      _$GlobalSearchResponseFromJson(json);
}

@JsonSerializable()
class GlobalSearch {
  int id;
  String label;
  String fulllabel;
  String path;
  int plateid;
  int pid;
  @JsonKey(defaultValue: false)
  bool selected;
  GlobalSearch({this.id, this.label, this.fulllabel, this.path,this.plateid,this.pid});

  factory GlobalSearch.fromJson(Map<String, dynamic> json) =>
      _$GlobalSearchFromJson(json);
}


@JsonSerializable()
class ModulesResponse {
  String requestId;
  int code;
  String msg;
  List<Module> data;

  ModulesResponse({this.requestId, this.code, this.msg, this.data});

  factory ModulesResponse.fromJson(Map<String, dynamic> json) =>
      _$ModulesResponseFromJson(json);
}

@JsonSerializable()
class Module {
  int id; //板块id
  String section; //板块名称
  int mode; //0:grid, 1:list

  Module({this.id, this.section, this.mode});

  factory Module.fromJson(Map<String, dynamic> json) => _$ModuleFromJson(json);
}
////

///////////
@JsonSerializable()
class CategoriesResponse {
  String requestId;
  int code;
  String msg;
  List<Category> data;

  CategoriesResponse({this.code, this.msg, this.data, this.requestId});

  factory CategoriesResponse.fromJson(Map<String, dynamic> json) =>
      _$CategoriesResponseFromJson(json);
}

@JsonSerializable()
class Category {
  int id;
  String label;
  String path;
  bool filter;

  bool selected = false;

  Category(
      {this.id, this.label, this.path, this.filter, this.selected = false});

  factory Category.fromJson(Map<String, dynamic> json) =>
      _$CategoryFromJson(json);
}

/////////
@JsonSerializable()
class FiltersResponse {
  String requestId;
  int code;
  String msg;
  List<Filter> data;

  FiltersResponse({this.requestId, this.code, this.msg, this.data});

  factory FiltersResponse.fromJson(Map<String, dynamic> json) =>
      _$FiltersResponseFromJson(json);
}

@JsonSerializable()
class Filter {
  String markname;
  List<String> values;

  Filter({this.markname, this.values});

  factory Filter.fromJson(Map<String, dynamic> json) => _$FilterFromJson(json);
}
/////////

@JsonSerializable()
class ResourcesResponse {
  String requestId;
  int code;
  String msg;
  ResourceList data;

  ResourcesResponse({this.requestId, this.code, this.msg, this.data});

  factory ResourcesResponse.fromJson(Map<String, dynamic> json) =>
      _$ResourcesResponseFromJson(json);
}

@JsonSerializable()
class ResourceList {
  int count; // 这个总数是剩余还有多少记录。如果第一次查，这个数目是剩余所有记录的总数。
  int pageIndex;
  int pageSize;
  List<Resource> list;

  ResourceList({this.count, this.pageIndex, this.pageSize, this.list});

  factory ResourceList.fromJson(Map<String, dynamic> json) =>
      _$ResourceListFromJson(json);
}

@JsonSerializable()
class Resource {
  int id;
  String url; //缩略图url
  int type; //  资源类型（是1--多组图片，2---教学，3---环拍多帧  4---3D  5---多摸）
  String title; //资源标题
  int page;
  String album;
  String series;
  String author;


  Resource({this.url, this.type, this.id, this.title,this.page, this.album, this.series, this.author});

  factory Resource.fromJson(Map<String, dynamic> json) =>
      _$ResourceFromJson(json);
}
